package com.ynny.Controller;


import com.ynny.Entity.Hobby;
import com.ynny.Entity.R;
import com.ynny.Entity.Users;
import com.ynny.Entity.UsersDTO;
import com.ynny.Service.HobbyService;
import com.ynny.Service.UsersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
@RequestMapping("/user")
    public class UsersController {
        @Autowired
        private UsersService usersService;

        @Autowired
        private HobbyService hobbyService;

        @PostMapping("/login")
        public R login(@RequestBody Users users){
            //判断用户名和密码释放为空
            if (users.getNumber() == null || users.getPassword() == null){
                return R.error("用户名或密码不能为空");
            }
            return usersService.login(users);
        }


        @PostMapping("/register")
        public R register(@RequestBody UsersDTO usersDTO) {
            Users users = usersDTO.getUsers();//取出用户表信息
            List<Hobby> hobbies = usersDTO.getHobbies();//取出兴趣爱好信息

            if (users == null) {
                return R.error("用户信息不能为空");
            } else if (hobbies == null || hobbies.isEmpty()) {
                return R.error("兴趣爱好不能为空");
            }
            String number = users.getNumber();
            System.out.println(number);

            int i = usersService.addUserTable(users.getNumber(), users.getPassword(), users.getIdentity(), users.getSex());
            System.out.println(i);
            System.out.println(i);
            if (i == 0) {
                return R.error("注册失败，用户信息添加失败");
            } else {
                int i1 = hobbyService.addHobbyTable(users.getNumber(), hobbies);
                if (i1 == 0) {
                    return R.error("注册失败，兴趣爱好添加失败");
                } else {
                    return R.ok(users);
                }
            }
        }

    }

